//get的基础自定义
import {
    HOST
} from './host'
import {
    Toast
} from 'vant'
import back_to_login from "@/services/backToLogin.service.js"

const loadingdelay = 2500

// 权限不足，返回登录页
function backToLogin() {
    back_to_login()
}


function err_dispose(err) {
    //异常处理函数
    // console.info('错误', err)
    if (err.status == 500) {
        Toast({
            message: '请求失败请检查网络',
            position: 'center',
            duration: 2000
        })
    } else {
        Toast({
            message: '请求失败没返回',
            position: 'center',
            duration: 2000
        })
    }
}

function res_dispose(res, resolve, reject) {
    //自定义业务返回信息判断
    let data = res.data
    if (data.code == 201) {
        Toast({
            message: data.msg,
            position: 'center',
            duration: 2000
        })
    } else if (data.code == 500) {
        if (data.msg.indexOf('jwt') != -1) {
            backToLogin()
        } else {
            Toast({
                message: data.msg,
                position: 'center',
                duration: 2000
            })
        }
    }


}


import qs from 'qs'; //请求参数处理库
import axios from 'axios' //请求库
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; //请求头
axios.defaults.baseURL = HOST; //url公共前缀

export default {
    get: function (url, info) {
        //get请求    
        return new Promise(function (resolve, reject) {
            let delay = setTimeout(
                () => {
                    Toast.loading({
                        duration: 0, // 持续展示 toast
                        forbidClick: true, // 禁用背景点击
                        // loadingType: 'spinner',
                        message: ''
                    });
                },
                loadingdelay

            )

            //get请求
            axios.get(url, {
                params: info
            }).then(
                res => {
                    resolve(res)
                    // console.log(res)
                    res_dispose(res)
                    clearTimeout(delay)
                    Toast.clear()


                }
            ).catch(
                err => {
                    clearTimeout(delay)
                    Toast.clear()
                    err_dispose(err) //错误处理
                    reject(err)

                }
            )

        })

    },
    post: function (url, info) {
        //post请求
        return new Promise(function (resolve, reject) {

            let delay = setTimeout(
                () => {
                    Toast.loading({
                        duration: 0, // 持续展示 toast
                        forbidClick: true, // 禁用背景点击
                        // loadingType: 'spinner',
                        message: ''
                    });
                },
                loadingdelay
            )

            //get请求
            axios.post(url, qs.stringify(info), { //qs序列化参数 做成stringformdate格式
                headers: { //头部配置
                    'Accept': 'application/json, text/javascript, */*; q=0.01',
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                    'jwt': localStorage.getItem('httpToken') || '' // 用户鉴权
                },
                // params: info,
                // paramsSerializer: function(params) {
                //     return Qs.stringify(params, { arrayFormat: 'brackets' })
                // },
            }).then(
                res => {
                    clearTimeout(delay)
                    Toast.clear()
                    res_dispose(res)

                    // 登录token认证
                    if (res.data.code == 204 && res.data.msg.indexOf('token') != -1) {
                        localStorage.removeItem("httpToken");
                        localStorage.removeItem("userID");
                        localStorage.removeItem("userInfo");
                        console.log('token过期')

                        backToLogin()
                    }

                    resolve(res) //解析结果为json

                }
            ).catch(
                err => {
                    clearTimeout(delay)
                    Toast.clear()
                    err_dispose(err)
                    reject(err)
                }
            )
        })

    },
    formAjax: function (url, formData) {
        return new Promise(function (resolve, reject) {

            let delay = setTimeout(
                () => {
                    Toast.loading({
                        duration: 0, // 持续展示 toast
                        forbidClick: true, // 禁用背景点击
                        // loadingType: 'spinner',
                        message: ''
                    });
                },
                loadingdelay

            )

            //get请求
            axios.post(url, formData, { //qs序列化参数 做成stringformdate格式
                headers: { //头部配置
                    'Content-Type': 'multipart/form-data'
                },

                // params: info,
                // paramsSerializer: function(params) {
                //     return Qs.stringify(params, { arrayFormat: 'brackets' })
                // },
            }).then(
                res => {

                    clearTimeout(delay)
                    Toast.clear()
                    resolve(res) //解析结果为json
                }
            ).catch(
                err => {
                    clearTimeout(delay)
                    Toast.clear()
                    err_dispose(err)
                    reject(err)
                }
            )
        })
    }
}